package com.coolbi.common.util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JsonUtil {

	private static final String JACKSON_OBJECT_MAPPER_BEAN_NAME = "jacksonObjectMapper";// jackson
																						// ObjectMapper
																						// Bean名称

	public static ObjectMapper getMapper() {
		return (ObjectMapper) SpringUtil
				.getBean(JACKSON_OBJECT_MAPPER_BEAN_NAME);
	}

	// 将对象转换为JSON字符串
	public static String toJson(Object object) {
		ObjectMapper mapper = getMapper();
		try {
			return mapper.writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 将对象转换为JSON字符串
	 * 
	 * @param object
	 * @return
	 */
	public static String toJsonByObject(Object object) {
		JSONObject jsonArray = JSONObject.fromObject(object);
		return jsonArray.toString();
	}
	
	/**
	 * 将对象转换为JSON字符串
	 * 
	 * @param object
	 * @return
	 */
	public static String toJsonByArray(Object object) {
		JSONArray jsonArray = JSONArray.fromObject(object);
		return jsonArray.toString();
	}
	
	/**
	 * 将对象转换为JSON字符串
	 * 
	 * @param object
	 * @return
	 */
	public static String toJsonByObject(Object object,final String[] fields) {
		JsonConfig jsonConfig = new JsonConfig();
		
		jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				for (String propName : fields) {
					if (name.equals(propName)) {
						return false;
					}
				}
				
				return true;
			}
		});

		JSONObject jsonArray = JSONObject.fromObject(object, jsonConfig);

		return jsonArray.toString();
	}
	
	/**
	 * 将对象转换为JSON字符串
	 * 
	 * @param object
	 * @return
	 */
	public static String toJsonByObject(Object object,String dateFormart) {
		JsonConfig jsonConfig = new JsonConfig();

		jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor(dateFormart)); 
		
		JSONObject jsonArray = JSONObject.fromObject(object, jsonConfig);

		return jsonArray.toString();
	}
	
	/**
	 * 将数组转换为JSON字符串
	 * 
	 * @param object
	 * @return
	 */
	public static String toJsonByArray(Object object,String dateFormart) {
		JsonConfig jsonConfig = new JsonConfig();

		jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor(dateFormart)); 
		
		JSONArray jsonArray = JSONArray.fromObject(object, jsonConfig);

		return jsonArray.toString();
	}
	
	/**
	 * 将数组转换为JSON字符串
	 * 
	 * @param object
	 * @return
	 */
	public static String toJsonByArray(Object object,final String[] fields) {
		JsonConfig jsonConfig = new JsonConfig();
		
		jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				for (String propName : fields) {
					if (name.equals(propName)) {
						return false;
					}
				}
				return true;
			}
		});

		JSONArray jsonArray = JSONArray.fromObject(object, jsonConfig);

		return jsonArray.toString();
	}
	
	/**
	 * 将数组转换为JSON字符串
	 * 
	 * @param object
	 * @return
	 */
	public static String toJsonByArray(Object object,final String[] fields,String dateFormart) {
		JsonConfig jsonConfig = new JsonConfig();
		
		jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				for (String propName : fields) {
					if (name.equals(propName)) {
						return false;
					}
				}
				
				return true;
			}
		});

		jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor(dateFormart)); 
		
		JSONArray jsonArray = JSONArray.fromObject(object, jsonConfig);

		return jsonArray.toString();
	}
	
	/**
	 * 将数组转换为JSON字符串
	 * 
	 * @param object
	 * @return
	 */
	public static String toJsonByObject(Object object,final String[] fields,String dateFormart) {
		JsonConfig jsonConfig = new JsonConfig();
		
		jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				for (String propName : fields) {
					if (name.equals(propName)) {
						return false;
					}
				}
				
				return true;
			}
		});

		jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor(dateFormart)); 
		
		JSONObject jsonArray = JSONObject.fromObject(object, jsonConfig);

		return jsonArray.toString();
	}
	
	/**
	 * 将数组转换为JSON字符串，并对可能引起死循环的数据进行排除
	 * @param object
	 * @param fields
	 * @param excludeds
	 * @return
	 */
	public static String toJsonByArrayExcept(Object object,final String[] fields,final String[] excludeds) {
		JsonConfig jsonConfig = new JsonConfig();
		
		jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				
				//找到相同的，则进行排除
				if(excludeds!=null){
					for (String propName : excludeds) {
						if (name.equals(propName)) {
							return true;
						}
					}
				}
				
				//如果指定的字段列表为Null，则表示全部不进行过滤
				if(fields==null){
					return false;
				}else{
					for (String string : fields) {
						if (name.equals(string)) {
							return false;
						}
					}
				}
	
				return true;
			}
		});
		
		JSONArray jsonArray = JSONArray.fromObject(object, jsonConfig);

		return jsonArray.toString();
	}
	
	/**
	 * 将数组转换为JSON字符串，并对可能引起死循环的数据进行排除
	 * @param object
	 * @param fields
	 * @param excludeds
	 * @return
	 */
	public static String toJsonByArrayExcept(Object object,final String[] fields,final String[] excludeds,String dateFormart) {
		JsonConfig jsonConfig = new JsonConfig();
		
		jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				
				//找到相同的，则进行排除
				if(excludeds!=null){
					for (String propName : excludeds) {
						if (name.equals(propName)) {
							return true;
						}
					}
				}
				
				//如果指定的字段列表为Null，则表示全部不进行过滤
				if(fields==null){
					return false;
				}else{
					for (String string : fields) {
						if (name.equals(string)) {
							return false;
						}
					}
				}
	
				return true;
			}
		});
		
		jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor(dateFormart)); 

		JSONArray jsonArray = JSONArray.fromObject(object, jsonConfig);

		return jsonArray.toString();
	}
	
	/**
	 * 将对象转换为JSON字符串，并对可能引起死循环的数据进行排除
	 * @param object
	 * @param fields 为Null时表示获得所有
	 * @param excludeds
	 * @return
	 */
	public static String toJsonByObjectExcept(Object object,final String[] fields,final String[] excludeds) {
		JsonConfig jsonConfig = new JsonConfig();
		
		jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				
				//找到相同的，则进行排除
				if(excludeds!=null){
					for (String propName : excludeds) {
						if (name.equals(propName)) {
							return true;
						}
					}
				}
				
				//如果指定的字段列表为Null，则表示全部不进行过滤
				if(fields==null){
					return false;
				}else{
					for (String string : fields) {
						if (name.equals(string)) {
							return false;
						}
					}
				}
	
				return true;
			}
		});

		JSONObject jsonArray = JSONObject.fromObject(object, jsonConfig);

		return jsonArray.toString();
	}

	/**
	 * 将对象转换为JSON字符串，并对可能引起死循环的数据进行排除
	 * @param object
	 * @param fields 为Null时表示获得所有
	 * @param excludeds
	 * @return
	 */
	public static String toJsonByObjectExcept(Object object,final String[] fields,final String[] excludeds,String dateFormart) {
		JsonConfig jsonConfig = new JsonConfig();
		
		jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				
				//找到相同的，则进行排除
				if(excludeds!=null){
					for (String propName : excludeds) {
						if (name.equals(propName)) {
							return true;
						}
					}
				}
				
				//如果指定的字段列表为Null，则表示全部不进行过滤
				if(fields==null){
					return false;
				}else{
					for (String string : fields) {
						if (name.equals(string)) {
							return false;
						}
					}
				}
	
				return true;
			}
		});
		jsonConfig.registerJsonValueProcessor(Date.class,new JsonDateValueProcessor(dateFormart)); 

		JSONObject jsonArray = JSONObject.fromObject(object, jsonConfig);
		
		return jsonArray.toString();
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T toObjectByJsonLib(String object, Class<T> clazz) {
		System.out.println("JsonUtil 196 开始转换json对象");
		JSONObject jsonObject = JSONObject.fromObject(object);
		System.out.println("JsonUtil 198 json对象转换完毕，json对象转换bean对象开始");
		Object bean = JSONObject.toBean(jsonObject, HashMap.class);
		System.out.println("json转换bean完毕");
		return (T) bean;
	}

	// 将JSON字符串转换为对象
	public static <T> T toObject(String json, Class<T> clazz) {
		ObjectMapper mapper = getMapper();
		try {
			return mapper.readValue(json, clazz);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	// 将JSON字符串转换为对象
	public static <T> T toObject(String json, Class<T> clazz,String dateformat) {
		ObjectMapper mapper = getMapper();
		SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
		mapper.setDateFormat(sdf);
		try {
			return mapper.readValue(json, clazz);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 将JSON字符串转换为对象
	public static <T> T toObject(String json, TypeReference<?> typeReference)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = getMapper();
		return mapper.readValue(json, typeReference);
	}

}